package com.mh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mh.dao.DoctorInfoDao;
import com.mh.pojo.DoctorInfo;
import com.mh.service.DoctorInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * Date:2023/4/13
 * author:zmh
 * description: 医生信息业务层实现类
 **/

@Service
public class DoctorInfoServiceImpl extends ServiceImpl<DoctorInfoDao, DoctorInfo> implements DoctorInfoService {
    @Autowired
    private DoctorInfoDao doctorInfoDao;

    /**
     * 获取在线医生列表
     * @return 在线医生列表
     */
    @Override
    public List<DoctorInfo> getOnlineDoctor() {
        LambdaQueryWrapper<DoctorInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DoctorInfo::getOnlineStatus,1);
        List<DoctorInfo> doctorInfos = doctorInfoDao.selectList(queryWrapper);
        return doctorInfos;
    }

    /**
     * 鉴权登录
     * @param userName 姓名
     * @param password 密码
     * @return 返回执行结果
     */
    @Override
    public String checkLoginByDoctor(String userName, String password) {
        // 先将传入的密码进行md5加密后再进行进行验证
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        // 构造条件查询医生对象信息
        LambdaQueryWrapper<DoctorInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DoctorInfo::getName, userName);
        DoctorInfo doctorInfo = doctorInfoDao.selectOne(queryWrapper);
        // 进行登录检查判断
        if (doctorInfo == null){
            return "登录失败！输入的医生未录入";
        } else if (!doctorInfo.getPassword().equals(md5Password)) {
            return "登录失败！输入的账号密码不匹配";
        }
        return "登录成功!";
    }
}
